package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.powermaster.server.dao.DeviceSpecDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.DeviceAirEntity;

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.*;

public class DeviceSpecAirImpl implements DeviceSpecDao {
    @Override
    public List<Map<String, String>> findAll() {
        String sql = "SELECT * FROM device_air WHERE status = 1 ";
        List<Map<String, String>> list = JdbcUtil.queryForList_Map(sql, null);
        return list;
    }

    @Override
    public Map<String, String> findOne(String deviceId) {
        String sql = "SELECT * FROM device_air WHERE status = ? AND device_id = ?";
        Object[] params = new Object[2];
        params[0] = 1;
        params[1] = deviceId;
        Map<String, String> map = JdbcUtil.queryForMap(sql, params);
        if (map == null) {
            return new HashMap<>();
        }
        return map;
    }

    @Override
    public boolean insert(Map<String, String> map) throws SQLException {
        String sql = "REPLACE INTO device_air(device_id,refrigerating_capacity,rated_voltage,heating_capacity,humidification_capacity," +
            "crygen,weight,compressor_rated_power,heater_rated_power,fan_rated_power,humidifier_rated_power,safety_valve_pressure," +
            "overheating_temperature,turn_off_pressure,name,manufacturer,manufacture_date,status," +
            "air_set_temperature_upper_limit,air_set_temperature_lower_limit) " +
            "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,1,?,?)";
        Object[] params = new Object[19];
        params[0] = map.get("device_id");
        params[1] = map.get("refrigerating_capacity");
        params[2] = map.get("rated_voltage");
        params[3] = map.get("heating_capacity");
        params[4] = map.get("humidification_capacity");
        params[5] = map.get("crygen");
        params[6] = map.get("weight");
        params[7] = map.get("compressor_rated_power");
        params[8] = map.get("heater_rated_power");
        params[9] = map.get("fan_rated_power");
        params[10] = map.get("humidifier_rated_power");
        params[11] = map.get("safety_valve_pressure");
        params[12] = map.get("overheating_temperature");
        params[13] = map.get("turn_off_pressure");
        params[14] = map.get("name");
        params[15] = map.get("manufacturer");
        //params[16] = DateUtil.stringToDate(map.get("manufacture_date"));
        params[16] = map.get("manufacture_date");
        params[17] = map.get("air_set_temperature_upper_limit");
        params[18] = map.get("air_set_temperature_lower_limit");

        boolean result = JdbcUtil.update1(sql, params);
        return result;
    }

    @Override
    public boolean update(Map<String, String> map) throws SQLException {
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("UPDATE device_air SET ");
        ArrayList<String> paramList = new ArrayList<>();
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry) iterator.next();
            String key = entry.getKey();
            Field field = null;
            try {
                field = DeviceAirEntity.class.getDeclaredField(key);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if (key != null && !"device_id".equals(key) && field != null) {
                sqlBuffer.append(key);
                sqlBuffer.append("=?,");
                paramList.add(entry.getValue());
            }
        }
        sqlBuffer.append("status=1");
        sqlBuffer.append(" WHERE device_id=?");
        paramList.add(map.get("device_id"));
        String sql = sqlBuffer.toString();
        Object[] params = paramList.toArray();
        boolean result = JdbcUtil.update1(sql, params);
        return result;
    }

    @Override
    public boolean delete(String deviceId) throws SQLException {
        String sql = "UPDATE device_air SET status = 0 WHERE 1 = ? and device_id = ?";
        Object[] params = new Object[2];
        params[0] = 1;
        params[1] = deviceId;
        boolean result = JdbcUtil.update1(sql, params);
        return result;
    }

    @Override
    public boolean delete(String[] sns) throws SQLException {
        StringBuffer sqlBuffer = new StringBuffer();
        int length = sns.length;
        sqlBuffer.append("UPDATE device_air SET status = 0 WHERE divice_id IN ");
        sqlBuffer.append("(");
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                sqlBuffer.append("?)");
            } else {
                sqlBuffer.append("?,");
            }
        }
        String sql = sqlBuffer.toString();
        boolean result = JdbcUtil.update1(sql, sns);
        return result;
    }
}
